package cn.cp.baseframe;

import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.function.ConverterFileName;

import java.util.Collections;

/**
 * 配置文档地址
 * https://baomidou.com/pages/981406/
 */
public class CodeGenerator {

    private static void run() {
        FastAutoGenerator.create("jdbc:mysql://127.0.0.1:3306/base?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8",
                "root", "123456")
                .globalConfig(builder -> {
                    builder.author("cp") // 设置作者
                            .enableSwagger() // 开启 swagger 模式
                            .outputDir("src\\main\\java"); // 指定输出目录
                })
                .packageConfig(builder -> {
                    builder.parent("cn.cp.baseframe") // 设置父包名
                            .moduleName("") // 设置父包模块名
                            .entity("model.domain")
                            .mapper("mapper")
                            .service("service")
                            .serviceImpl("service.impl")
                            .controller("controller")
                            .pathInfo(Collections.singletonMap(OutputFile.xml, "src\\main\\resources\\mapper")); // 设置mapperXml生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude("user") // 设置需要生成的表名
                            .addTablePrefix("t_", "c_"); // 设置过滤表前缀
                    builder.mapperBuilder()
                            .enableBaseResultMap()
                            .enableBaseColumnList();
                    builder.entityBuilder()
                            .enableLombok()
                            .convertFileName(new ConverterFileName() {
                                @Override
                                public String convert(String entityName) {
                                    return entityName + "DO";
                                }
                            });
                })
                .templateEngine(new FreemarkerTemplateEngine()) // 使用Freemarker引擎模板，默认的是Velocity引擎模板
                .execute();

    }

    public static void main(String[] args) {
        CodeGenerator.run();
    }
}
